<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
import { AppTypeEnum, APPTYPE_TEXT_MAP } from '@/enums';
defineOptions({ name: 'AppForm' });
const _props = defineProps(props);
const setIcon = fileName => {
  form.icon = fileName;
  showSelect.value = false;
};
const handleSelectIconShow = () => {
  showSelect.value = true;
};
const showSelect = ref(false);
const loading = ref(false);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.type == AppTypeEnum.Catalog) {
        form.component = null;
        form.showCorner = false;
      }
      loading.value = true;
      window.$api.app.save(form).then(res => {
        loading.value = false;
        ElMessage.success('保存成功!');
        useStore.window.windowRemove('AppForm' + (form.id ?? ''));
      });
    }
  });
};
const appCatalogs = ref([]);
const icons = ref([]);
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  pId: null,
  type: AppTypeEnum.App as number,
  name: null,
  component: null,
  license: null,
  icon: null,
  showCorner: false,
  width: null,
  height: null,
  sort: null,
  remark: null,
});
const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
});
/** 初始化 */
const init = async () => {
  loading.value = true;
  await window.$api.app.getNoResourceApps().then(res => {
    appCatalogs.value = res.data;
  });
  await window.$api.attachment.getAllSystemIcons().then(res => {
    icons.value = res.data;
  });
  if (_props.window.params) {
    if (_props.window.params.id) {
      await detail();
    }
  }
  loading.value = false;
};
const detail = async () => {
  await window.$api.app.detail(_props.window.params.id).then(res => {
    form.id = res.data.id;
    form.pId = res.data.pId;
    form.type = res.data.type;
    form.name = res.data.name;
    form.component = res.data.component;
    form.license = res.data.license;
    form.icon = res.data.icon;
    form.showCorner = res.data.showCorner;
    form.width = res.data.width;
    form.height = res.data.height;
    form.sort = res.data.sort;
    form.remark = res.data.remark;
  });
};
onMounted(() => {
  init();
});
</script>

<template>
  <windows-window :window="window" v-loading="loading" element-loading-background="rgba(255, 255, 255, 0.6)">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级应用" prop="pId">
            <el-tree-select
              v-model="form.pId"
              node-key="id"
              :data="appCatalogs"
              :props="{
                children: 'children',
                label: 'name',
              }"
              :expand-on-click-node="false"
              highlight-current
              default-expand-all
              check-strictly
            >
              <template #default="scope">
                <div class="custom-node">
                  <el-image :src="scope.data.icon" style="width: 20px" fit="contain" />
                  <span>{{ scope.node.label }}</span>
                </div>
              </template>
            </el-tree-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type" :disabled="form.id ? true : false">
              <el-radio v-for="key in Object.keys(APPTYPE_TEXT_MAP)" :label="parseInt(key)">{{ APPTYPE_TEXT_MAP[key] }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" :min="0" controls-position="right" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="form.type != AppTypeEnum.Catalog">
        <el-col :span="24">
          <el-form-item label="许可证" prop="license">
            <el-input v-model="form.license" placeholder="请输入许可证" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="form.type == AppTypeEnum.App">
        <el-col :span="12">
          <el-form-item label="组件" prop="component">
            <el-input v-model="form.component" placeholder="请输入组件" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角落显示" prop="showCorner">
            <el-switch v-model="form.showCorner" inline-prompt active-icon="Check" inactive-icon="Close" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="form.type == AppTypeEnum.App">
        <el-col :span="12">
          <el-form-item label="窗体宽度" prop="width">
            <el-input-number v-model="form.width" placeholder="请输入窗体宽度" :controls="false" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="窗体高度" prop="height">
            <el-input-number v-model="form.height" placeholder="请输入窗体高度" :controls="false" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="form.type != AppTypeEnum.Resource">
        <el-col :span="24">
          <el-form-item label="图标" prop="icon">
            <el-input v-model="form.icon" placeholder="请选择图标" readonly @click="handleSelectIconShow()">
              <template #prepend><el-image v-if="form.icon" :src="form.icon" style="display: flex; width: 20px" fit="contain" lazy /></template>
            </el-input>
            <div class="select-icon" v-if="showSelect">
              <el-image v-for="icon in icons" :src="icon" style="width: 40px" fit="contain" @click="setIcon(icon)" :class="{ active: form.icon == icon }" />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
